<template>
    <div class="container-box">
        
        <p class="title"><slot name='title'></slot></p>
        <search ref="search" :btn_text="btn_text" @add_patient="add_patient" @search="search" />
        <div class="select_date" v-show="select_date_isShow">
            <span>选择日期 </span>
            <input type="date" name="start" id="" v-model="date.startDate" @keydown.enter="search($refs.search.patient)">
            <input type="date" name="end" id="" v-model="date.endDate" @keydown.enter="search($refs.search.patient)">
            <span style="marginLeft:10px" v-show="date.startDate">{{ date.startDate }} 至 {{ date.endDate }}</span>
            <!-- <span>排序方式</span> -->
            <span style="marginLeft:10px;color:red" v-show="err_msg">{{ err_msg }}</span>
        </div>
        <slot name="otherOptions">
        </slot>
        <slot name="container">
        </slot>
    </div>
</template>

<script>
import search from '@/components/search.vue'
import vue from '@/main.js'
export default {
    props:['select_date_isShow','btn_text'],
    data(){
        return{
            patient:{},
            patientInfo:{},
            date:{
                startDate:'',
                endDate:''
            },
            // err_msg:""
            server_return_max_size:120,  // 服务器一次性最多返回120条记录
        }
    },
    components:{
        search
    },
    methods:{
        add_patient(){
            // let add_patienta=null
            let add_patient = this.$router.resolve("/add_patient")
            if(this.btn_text=="添加病历信息"){
                add_patient = this.$router.resolve("/add_medical_record")
            }else if(this.btn_text=="添加看病记录"){
                add_patient = this.$router.resolve("/add_treat_record")
            }
            
            let new_w = 1120, new_h = 630
            let w = (window.outerWidth-new_w)/2
            let h = (window.outerHeight-new_h)/2
            window.open(add_patient.href,'_blank',`channelmode=yes,top=${h},left=${w},width=${new_w},height=${new_h}`)
            
        },
        search(patient,current_page){
            let only_search_patient=false
            if(this.$route.path == "/patientInfo"){
                only_search_patient=true
            }
            this.$store.state.global_var.search={patient:patient,date:this.date}
            if(this.date.startDate>this.date.endDate || (!this.date.startDate && this.date.endDate)){
                return
            }
            this.$http({
                method:'post',
                url:'/ajax/api/search_patient',
                data:{
                    patient:patient,
                    date:this.date,
                    only_search_patient:only_search_patient,
                    current_page:current_page || 0,
                    page_size:this.page_size || 15
                }
            }).then(data=>{
                if(typeof data.data == "string"){
                    this.$toast(data.data)
                    return
                }
                // this.add_patient()
                this.arrange_data(data)
                // 获取到数据后，保存数据
                // this.$store.state.show_list = data.data
                // this.$store.state.patient = data.data.patient_list
                // // 如果用户是按日期查询数据的话，要对数据进行整理
                // this.$store.state.patient.forEach((item,index)=>{
                //     this.$store.state.patient[index].medical_record=[]
                //     this.$store.state.patient[index].treat_record=[]
                //     // 根据生日，计算出年龄
                //     this.$store.state.patient[index]["age"]= this.$my_tools.getAge(item["birthday"])
                //     this.$store.state.show_list.medical_record.forEach((m_item,m_index)=>{
                //         if (item.id==m_item.patient_id){
                //             this.$store.state.patient[index].medical_record.push(m_item)
                //             // this.$store.state.show_list.medical_record[m_index].medical_insurance=item.medical_insurance
                //         }
                //     })
                //     this.$store.state.show_list.treat_record.forEach((m_item,m_index)=>{
                //         if (item.id==m_item.patient_id){
                //             this.$store.state.patient[index].treat_record.push(m_item)
                //         }
                //     })
                // })
                this.$emit("update_patient")
                console.log(data.data);
                console.log(this.$store.state.patient);
                if(this.$route.path=="/add_patient"){
                    this.$router.push("/patientInfo")
                }
            })
        },
        arrange_data(data){
            // 获取到数据后，保存数据
            if (data.data.medical_record_more=="yes"){
                data.data.medical_record=data.data.medical_record.slice(0,this.server_return_max_size)
            }
            if (data.data.treat_record_more=="yes"){
                data.data.medical_record=data.data.medical_record.slice(0,this.server_return_max_size)
            }
            // 如果用户是按日期查询数据的话，要对数据进行整理
            data.data.patient_list.forEach((item,index)=>{
                data.data.patient_list[index].medical_record=[]
                data.data.patient_list[index].treat_record=[]
                // 根据生日，计算出年龄
                data.data.patient_list[index]["age"]= this.$my_tools.getAge(item["birthday"])
                if (data.data.medical_record){
                    data.data.medical_record.forEach((m_item,m_index)=>{
                        if (item.id==m_item.patient_id){
                            data.data.patient_list[index].medical_record.push(m_item)
                            // this.$store.state.show_list.medical_record[m_index].medical_insurance=item.medical_insurance
                        }
                    })
                }
                if(data.data.treat_record){
                    data.data.treat_record.forEach((m_item,m_index)=>{
                        if (item.id==m_item.patient_id){
                            data.data.patient_list[index].treat_record.push(m_item)
                        }
                    })
                }
            })
            this.$store.state.show_list=[]
            this.$store.state.patient=[]
            this.$store.state.show_list[0]=data.data
            this.$store.state.patient[0]=data.data.patient_list
            this.$store.state.global_var.current_page=data.data.current_page
            // console.log(data.data.medical_record.slice(0,this.server_return_max_size));
        },
        
    },
    computed:{
        err_msg:{
            get(){
                if(this.date.startDate>this.date.endDate){
                    return this.err_msg="开始日期要 早于等于 结束日期"
                }else if((!this.date.startDate && this.date.endDate)){
                    return this.err_msg="请选择 开始日期"
                }else{
                    return this.err_msg=""
                }
            },
            set(val){
                
            }
        }
    }
    ,
    mounted(){
        if(this.$store.state.global_var.search){
            this.date=this.$store.state.global_var.search.date
        }
    }
}
</script>

<style lang='scss' scoped>
.container-box{
    display: flex;
    flex-direction: column;
    // align-items:flex-start;
    // padding-left:20px;
    .title{
        font-weight: bolder;
        margin:10px 0;
        text-align: center;
    }
    .select_date{
        // width: 90%;
        padding:5px 0px ;
        font-size: 13px;
    }
    

}
</style>